/*
 * Copyright (C) 2008 Eric Bottard
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.liquidform;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.code.liquidform.internal.AliasFactory;
import com.google.code.liquidform.internal.Pair;

/**
 * Represents the optional <tt>ORDER BY</tt> part of a query. One an instance of
 * this class has been created using <i>eg</i>
 * {@link FromClause#orderBy(Object)}, additional sort parameters may be added
 * using the {@link #andThenBy(Object)} construct.
 */
public class OrderByClause extends AbstractClause {

	// Can't use LinkedHashMap<Object, Direction> because it uses equals() on
	// keys, we need ==
	private List<Pair<Object, SortDirection>> sortInstructions = new ArrayList<Pair<Object, SortDirection>>();

	/* default */OrderByClause(AbstractClause previousClause,
			Object firstInstruction, SortDirection firstDirection) {
		super(previousClause);
		sortInstructions.add(new Pair<Object, SortDirection>(firstInstruction,
				firstDirection, false));
	}

	public OrderByClause andThenBy(Number o, SortDirection dir) {
		sortInstructions.add(new Pair<Object, SortDirection>(o, dir, false));
		return this;
	}

	public OrderByClause andThenBy(Number o) {
		return andThenBy(o, SortDirection.ASC);
	}

	public OrderByClause andThenBy(Date o, SortDirection dir) {
		sortInstructions.add(new Pair<Object, SortDirection>(o, dir, false));
		return this;
	}

	public OrderByClause andThenBy(Date o) {
		return andThenBy(o, SortDirection.ASC);
	}

	public OrderByClause andThenBy(String o, SortDirection dir) {
		sortInstructions.add(new Pair<Object, SortDirection>(o, dir, false));
		return this;
	}

	public OrderByClause andThenBy(String o) {
		return andThenBy(o, SortDirection.ASC);
	}

	public OrderByClause andThenBy(Character o, SortDirection dir) {
		sortInstructions.add(new Pair<Object, SortDirection>(o, dir, false));
		return this;
	}

	public OrderByClause andThenBy(Character o) {
		return andThenBy(o, SortDirection.ASC);
	}

	// public OrderByClause andThenBy(Enum o, SortDirection dir) {
	// sortInstructions.add(new Pair<Object, SortDirection>(o, dir, false));
	// return this;
	// }
	//
	// public OrderByClause andThenBy(Enum o) {
	// return andThenBy(o, SortDirection.ASC);
	// }

	@Override
	StringBuilder addTo(StringBuilder buffer) {
		buffer.append(" order by ");

		int i = 0;
		for (Pair<Object, SortDirection> inst : sortInstructions) {
			buffer.append(AliasFactory.about(inst.first)
					.getTargetRepresentation());
			buffer.append(" ").append(inst.second.stringForm);
			if (++i < sortInstructions.size()) {
				buffer.append(", ");
			}
		}
		return buffer;
	}

}
